package com.haier.serviceedu.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.haier.serviceedu.dao.EduTeacherDao;
import com.haier.serviceedu.entity.InvoiceCheckInType;
import com.haier.serviceedu.service.EduTeacherService;
import com.haier.serviceedu.entity.EduTeacher;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;


/**
 * (EduTeacher)表服务实现类
 *
 * @author wangxu
 * @since 2021-11-11 17:03:03
 */
@Service("eduTeacherService")
public class EduTeacherServiceImpl implements EduTeacherService {
    @Resource
    private EduTeacherDao eduTeacherDao;

    @Value("${serviceurl.hccurl}")
        private String url;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public EduTeacher queryById(String id) {
        return this.eduTeacherDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<EduTeacher> queryAllByLimit(int offset, int limit) {
        return this.eduTeacherDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param eduTeacher 实例对象
     * @return 实例对象
     */
    @Override
    public EduTeacher insert(EduTeacher eduTeacher) {
        this.eduTeacherDao.insert(eduTeacher);
        return eduTeacher;
    }

    /**
     * 修改数据
     *
     * @param eduTeacher 实例对象
     * @return 实例对象
     */
    @Override
    public EduTeacher update(EduTeacher eduTeacher) {
        this.eduTeacherDao.update(eduTeacher);
        return this.queryById(eduTeacher.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.eduTeacherDao.deleteById(id) > 0;
    }

    /**
     * 发票信息校验  HTTP服务连接的使用问题
     *
     * @param invoiceCheckInType
     */
    @Override
    public String invoiceCheck(InvoiceCheckInType invoiceCheckInType) throws Exception {

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Authorization", "Basic MTU3NDI5OTY4ODQwODoxNzZkODA0NzVjOGM0YzRiYTBmMTg0YjUxOWM3ZDQwOA==");

        //处理参数信息
        JSONObject jsonObject0  =   new JSONObject();
        JSONObject jsonObject1  =   new JSONObject();
        List<JSONObject> jsonlist   =   new ArrayList<>();

        jsonObject0.put("invoiceCode",invoiceCheckInType.getInvoiceCode());
        jsonObject0.put("invoiceNumber",invoiceCheckInType.getInvoiceNumber());
        jsonObject0.put("totalAmout",invoiceCheckInType.getTotalAmout());
        jsonObject0.put("checkCode_6",invoiceCheckInType.getCheckCode_6());
        jsonObject0.put("billingDate",invoiceCheckInType.getBillingDate());
        jsonObject0.put("dataSource",invoiceCheckInType.getDataSource());
        jsonObject0.put("applicant",invoiceCheckInType.getApplicant());

//        jsonlist.add(jsonObject0);
//        jsonObject1.put("blpList",jsonlist);
//        System.out.println(jsonObject1);
        StringEntity entity = new StringEntity(jsonObject0.toJSONString(), "utf-8");

        httpPost.setEntity(entity);
        HttpResponse response = httpclient.execute(httpPost);


        HttpEntity result1=response.getEntity();
        String resultStr = EntityUtils.toString(result1);
        JSONObject jsonResult = JSONObject.parseObject(resultStr);

        System.out.println(jsonResult);

        return resultStr;
    }

    /**
     * 用hutool的API进行发票信息校验问题
     *
     * 这个接口是是怎么进行调用的  还需要自己有时间多思考一下 是怎么进行使用操作的
     *
     * @param invoiceCheckInType
     * @return
     */
    @Override
    public String invoiceChecks(InvoiceCheckInType invoiceCheckInType) {


        JSONObject jsonObject  =   new JSONObject();

        jsonObject.put("invoiceCode",invoiceCheckInType.getInvoiceCode());
        jsonObject.put("invoiceNumber",invoiceCheckInType.getInvoiceNumber());
        jsonObject.put("totalAmout",invoiceCheckInType.getTotalAmout());
        jsonObject.put("checkCode_6",invoiceCheckInType.getCheckCode_6());
        jsonObject.put("billingDate",invoiceCheckInType.getBillingDate());
        jsonObject.put("dataSource",invoiceCheckInType.getDataSource());
        jsonObject.put("applicant",invoiceCheckInType.getApplicant());


        String requestId = IdUtil.simpleUUID();
        String requestTime = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")));

        String result2 = HttpRequest.post(url)
                        .header(Header.AUTHORIZATION,"Basic MTU3NDI5OTY4ODQwODoxNzZkODA0NzVjOGM0YzRiYTBmMTg0YjUxOWM3ZDQwOA==")
                        .header(Header.CONTENT_TYPE, "application/json")
                        .body(String.valueOf(jsonObject))   //传入的参数信息格式错误 导致信息不能正常的进行识别操作
                        .timeout(10000)    //超时，毫秒
                        .header("system", "HCC") // 系统标识,固定值HCC
                        .header("requestId", requestId) // 请求id（保证每次请求唯一）
                        .header("requestTime", requestTime) // 请求时间（时间戳，单位：ms）
                        .execute().body();

        //String r = HttpUtil.post(url, JSONUtil.toJsonStr(returnJshParam));
        //cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(r);

        System.out.println(result2);
        return result2;
    }
}
